/*
 * iShopPlace Project.
 */
package ishopplace.model.service.impl;

import ishopplace.model.businessobject.Category;
import ishopplace.model.businessobject.Item;
import ishopplace.model.businessobject.Product;
import ishopplace.model.dao.CatalogDao;
import ishopplace.model.exception.iShopPlaceException;
import ishopplace.model.service.CatalogService;

import java.util.List;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * The implementation of the <code>CatalogService</code>.
 * <p>
 * Spring Framework is used to manage this service bean.
 * Since this class is not dependend on Spring API, 
 * it can be used outside the Spring IOC container.
 * 
 * @author <a href="mailto:derek@derekshen.com">Derek Y. Shen</a>
 * @see CatalogService
 */
public class CatalogServiceImpl implements CatalogService {
	//the logger for this class
	private Log logger = LogFactory.getLog(this.getClass());
	
	//the CatalogDao used
	private CatalogDao catalogDao;

	/**
	 * Set the <code>CatalogDao</code>.
	 * <p>
	 * It can be used by the Spring IOC container.
	 * 
	 * @param newCatalogDao the CatalogDao to be set
	 */
	public void setCatalogDao(CatalogDao newCatalogDao) {
		this.catalogDao = newCatalogDao;
	}
	
	public List getCategoryList() throws iShopPlaceException {
		try {
			return this.catalogDao.getCategoryList();
		} catch (Exception e) {
			String msg = "Could not get category list " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
	
	public Category getCategory(String categoryId) throws iShopPlaceException {
		try {
			return this.catalogDao.getCategory(categoryId);
		} catch (Exception e) {
			String msg = "Could not get category " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}		
	}
	
	public List getProductListByCategory(String categoryId) throws iShopPlaceException {
		try {
			return this.catalogDao.getProductListByCategory(categoryId);
		} catch (Exception e) {
			String msg = "Could not get product list " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
	
	public Product getProduct(String productId) throws iShopPlaceException {
		try {
			return this.catalogDao.getProduct(productId);
		} catch (Exception e) {
			String msg = "Could not get product " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
	
	public List getItemListByProduct(String productId) throws iShopPlaceException {
		try {
			return this.catalogDao.getItemListByProduct(productId);
		} catch (Exception e) {
			String msg = "Could not get item list by product id" + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
	
	public Item getItem(String itemId) throws iShopPlaceException {
		try {
			return this.catalogDao.getItem(itemId);
		} catch (Exception e) {
			String msg = "Could not get item " + e.toString();
			this.logger.error(msg, e);
			throw new iShopPlaceException(msg, e);
		}
	}
	
	public List searchProductList(List keywords) throws iShopPlaceException {
		if (keywords != null && !keywords.isEmpty()) {
			return this.catalogDao.searchProductList(keywords);
		}
		else {
			return new ArrayList();
		}
	}
}
